package com.xxwy.dependency.injection.aware;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.Aware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 基于{@link Aware} 接口回调的依赖函数
 * 生命周期
 * @author wy
 * @date 2020/4/27 13:35
 */
public class AwareInterfaceDependencyInjection implements BeanFactoryAware, ApplicationContextAware {

  private static BeanFactory beanFactory;
  private static ApplicationContext applicationContext;

  public static void main(String[] args) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    //Configuration Class (配置类) ->Spring Bean
    context.register(AwareInterfaceDependencyInjection.class);
    context.refresh();

    //true
    System.out.println(beanFactory == context.getBeanFactory());
    //true
    System.out.println(applicationContext == context);

    context.close();

  }

  @Override
  public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
    AwareInterfaceDependencyInjection.beanFactory = beanFactory;
  }


  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    AwareInterfaceDependencyInjection.applicationContext = applicationContext;
  }
}
